<?php

namespace app\common\model;

use app\admin\model\Admin;
use app\admin\model\shop\Message;
use think\Model;
use traits\model\SoftDelete;

class Bill extends Model
{

    use SoftDelete;


    // 表名
    protected $name = 'bill';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text'
    ];

    public const TYPE_ORDER = 100; // 订单数据产生的费用
    public const TYPE_RETURN = 200;// 支出

    public const TYPE = [
        self::TYPE_ORDER,
        self::TYPE_RETURN,
    ];

    public const TYPE_INCREASE = [
        self::TYPE_ORDER,
    ];
    public const TYPE_REDUCE = [
        self::TYPE_RETURN,
    ];


    public const TYPE_ARRAY = [
        self::TYPE_ORDER => 'Order income',
        self::TYPE_RETURN => 'Adjustment',
    ];

    public const TYPE_ARRAY_TEXT = [
        [
            'value' => self::TYPE_ORDER,
            'name' => 'Order income',
        ],
        [
            'value' => self::TYPE_RETURN,
            'name' => 'Adjustment',
        ],
    ];


    public const STATUS_NORMAL = 100; // 正常
    public const STATUS_INVALID = 200; // 作废

    public const STATUS_ARRAY = [
        self::STATUS_NORMAL => 'normal',
        self::STATUS_INVALID => 'Invalid',
    ];

    public function getStatusList()
    {
        return self::STATUS_ARRAY;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }


    public function shop()
    {
        return $this->belongsTo(Message::class, 'shop_id', 'id', [], 'LEFT')
            ->setEagerlyType(0);
    }


    public function admin()
    {
        return $this->belongsTo(Admin::class, 'admin_id', 'id', [], 'LEFT')
            ->setEagerlyType(0);
    }


    /**
     * Notes: 流水作废
     * User: chenLei
     * Date: 8/15/25
     * @param $order_id
     * @return true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function invalid($order_id)
    {
        $info = $this->where('order_id', $order_id)->find();
        if (!empty($info)) {
            $info->status = self::STATUS_INVALID;
            $info->save();
        }

        return true;
    }


    /**
     * Notes: 添加流水日志
     * User: chenLei
     * Date: 8/15/25
     * @param $params
     * @return true
     */
    public function add($params)
    {
        if (in_array($params['type'], self::TYPE_INCREASE)) {
            $be = 1;
        } else if (in_array($params['type'], self::TYPE_REDUCE)) {
            $be = 2;
        } else {
            $this->error = 'Type error';
            return false;
        }

        $this->save([
            'shop_id' => $params['shop_id'],
            'admin_id' => $params['admin_id'],
            'type' => $params['type'],
            'money' => $params['money'],
            'order_id' => $params['order_id'] ?? 0,
            'memo' => $params['memo'] ?? '',
            'status' => self::STATUS_NORMAL,
            'be' => $be,
        ]);

        return true;
    }
}
